﻿#include "WindowTitleBar.h"

#include <QMouseEvent>
#include <QPalette>
#include <QHBoxLayout>
#include <QSizePolicy>
#include <QTimer>

WindowTitleBar::WindowTitleBar(QWidget *parent) : QWidget(parent)
  , m_pIconButton(Q_NULLPTR)
  , m_pTitleLabel(Q_NULLPTR)
  , m_pMinimizeButton(Q_NULLPTR)
  , m_pMaximizeButton(Q_NULLPTR)
  , m_pCloseButton(Q_NULLPTR)
{
    QTimer::singleShot(0, this, SLOT(onInitialize()));
}

WindowTitleBar::~WindowTitleBar()
{
}

void WindowTitleBar::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        emit onMousePressed();
    }
    QWidget::mousePressEvent(event);
}

void WindowTitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        emit onMouseReleased();
    }
    QWidget::mouseReleaseEvent(event);
}

void WindowTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        emit onMouseDoubleClicked();
    }
    QWidget::mouseDoubleClickEvent(event);
}

void WindowTitleBar::setTitleText(const QString &m_text)
{
    if (m_pTitleLabel)
    {
        m_pTitleLabel->setText(m_text);
    }
}

QString WindowTitleBar::titleText() const
{
    if (m_pTitleLabel)
    {
        return m_pTitleLabel->text();
    }
    return QString();
}

void WindowTitleBar::setTitleTextAlignCenter(bool m_center)
{
    if (m_pTitleLabel)
    {
        if (m_center)
        {
            m_pTitleLabel->setAlignment(Qt::AlignCenter);
        }
        else
        {
            m_pTitleLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        }
    }
}

void WindowTitleBar::setTitleBarBackgroundColor(QColor m_color)
{
    QPalette m_palette;
    m_palette.setColor(QPalette::Background, m_color);
    setPalette(m_palette);
}

QColor WindowTitleBar::titleBarBackgroundColor() const
{
    QPalette m_palette = palette();
    QColor m_color = m_palette.color(QPalette::Background);
    return m_color;
}

void WindowTitleBar::setTitleBarHeight(int m_height)
{
    setFixedHeight(m_height > 0 ? m_height : TITLE_BAR_DEFAULT_HEIGHT);
}

int WindowTitleBar::titleBarHeight() const
{
    return height() > 0 ? height() : TITLE_BAR_DEFAULT_HEIGHT;
}

void WindowTitleBar::setIconButtonVisible(bool m_visible)
{
    if (m_pIconButton)
    {
        m_pIconButton->setVisible(m_visible);
    }
}

bool WindowTitleBar::iconButtonVisible() const
{
    if (m_pIconButton)
    {
        return m_pIconButton->isVisible();
    }
    return false;
}

void WindowTitleBar::setTitleLabelVisible(bool m_visible)
{
    if (m_pTitleLabel)
    {
        m_pTitleLabel->setVisible(m_visible);
    }
}

bool WindowTitleBar::titleLabelVisible() const
{
    if (m_pTitleLabel)
    {
        return m_pTitleLabel->isVisible();
    }
    return false;
}

void WindowTitleBar::setMinimizeButtonVisible(bool m_visible)
{
    if (m_pMinimizeButton)
    {
        m_pMinimizeButton->setVisible(m_visible);
    }
}

bool WindowTitleBar::minimizeButtonVisible() const
{
    if (m_pMinimizeButton)
    {
        return m_pMinimizeButton->isVisible();
    }
    return false;
}

void WindowTitleBar::setMaximizeButtonVisible(bool m_visible)
{
    if (m_pMaximizeButton)
    {
        m_pMaximizeButton->setVisible(m_visible);
    }
}

bool WindowTitleBar::maximizeButtonVisible() const
{
    if (m_pMaximizeButton)
    {
        return m_pMaximizeButton->isVisible();
    }
    return false;
}

void WindowTitleBar::setCloseButtonVisible(bool m_visible)
{
    if (m_pCloseButton)
    {
        m_pCloseButton->setVisible(m_visible);
    }
}

bool WindowTitleBar::closeButtonVisible() const
{
    if (m_pCloseButton)
    {
        return m_pCloseButton->isVisible();
    }
    return false;
}

void WindowTitleBar::onInitialize()
{
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    setFixedHeight(TITLE_BAR_DEFAULT_HEIGHT);
    QPalette m_palette;
    m_palette.setColor(QPalette::Background, TITLE_BAR_DEFAULT_BACKGROUND_COLOR);
    setAutoFillBackground(true);
    setPalette(m_palette);
    setMouseTracking(true);
    m_pIconButton = new QPushButton(this);
    m_pIconButton->setText("Icon");
    m_pTitleLabel = new QLabel(this);
    m_pTitleLabel->setMouseTracking(true);
    m_pTitleLabel->setText("Title");
    m_pMinimizeButton = new QPushButton(this);
    m_pMinimizeButton->setText("Minimize");
    m_pMaximizeButton = new QPushButton(this);
    m_pMaximizeButton->setText("Maximize/Restore");
    m_pCloseButton = new QPushButton(this);
    m_pCloseButton->setText("Close");
    QHBoxLayout *m_pTitleBarLayout = new QHBoxLayout(this);
    m_pTitleBarLayout->setContentsMargins(10, 0, 10, 0);
    m_pTitleBarLayout->setSpacing(10);
    setLayout(m_pTitleBarLayout);
    m_pTitleBarLayout->addWidget(m_pIconButton);
    m_pTitleBarLayout->addWidget(m_pTitleLabel);
    m_pTitleBarLayout->addWidget(m_pMinimizeButton);
    m_pTitleBarLayout->addWidget(m_pMaximizeButton);
    m_pTitleBarLayout->addWidget(m_pCloseButton);
    connect(m_pIconButton, SIGNAL(clicked()), this, SIGNAL(onIconButtonClicked()));
    connect(m_pMinimizeButton, SIGNAL(clicked()), this, SIGNAL(onMinimizeButtonClicked()));
    connect(m_pMaximizeButton, SIGNAL(clicked()), this, SIGNAL(onMaximizeButtonClicked()));
    connect(m_pCloseButton, SIGNAL(clicked()), this, SIGNAL(onCloseButtonClicked()));
    hide();
    emit windowTitleBarInitialized();
}
